﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Squadron.Core;
using Squadron.Misc;
using System.Diagnostics;
using Squadron.Widgets;
using System.Threading;

namespace Squadron
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

            SquadronContext.MainForm = this;
            SquadronContext.Utils.InitializeControls(this);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var addins = SquadronContext.Addins;

            foreach (SquadronAddin addin in addins)
            {
                SquadronItem item = new SquadronItem()
                {
                    Addin = addin
                };

                AddinList.Items.Add(item);
            }

            if (Debugger.IsAttached)
                if (Environment.MachineName == "HP")
                    UrlText.Text = "http://hp";
        }

        private void AddinList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AddinList.SelectedItem != null)
            {
                SquadronAddin addin = (AddinList.SelectedItem as SquadronItem).Addin;

                if (addin != null)
                {
                    SquadronContext.ActiveAddin = addin;

                    TitleLabel.Text = addin.Name;
                    TitleLabel.Tag = addin;

                    AddinControlContainer.Controls.Clear();
                    AddinControlContainer.Controls.Add(addin.UserControlInstance);

                    SquadronContext.Utils.InitializeControls(addin.UserControlInstance);

                    addin.UserControlInstance.Dock = DockStyle.Fill;

                    SetHelpLink(addin);
                }
            }
        }

        private void SetHelpLink(SquadronAddin addin)
        {
            helpbox.Tag = addin;

            bool valid = helpbox.Visible = !string.IsNullOrEmpty(addin.HelpUrl);
            helpbox.Left = TitleLabel.Left + TitleLabel.Width + 4;

            if (valid)
                new Thread(new ThreadStart(() =>
                    {
                        for (int i = 1; i <= 6; i++)
                        {
                            helpbox.Visible = !helpbox.Visible;
                            Thread.Sleep(100);
                        }

                        helpbox.Visible = !string.IsNullOrEmpty((helpbox.Tag as SquadronAddin).HelpUrl);
                    }
                    )).Start();
        }

        private void CanvasDataButton_Click(object sender, EventArgs e)
        {
            ShowCanvasDataForm();
        }

        public void ShowCanvasDataForm()
        {
            if (SquadronContext.CanvasDataForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                CanvasDataButton.Text = "Canvas Data (" + SquadronContext.CanvasDataForm.DataGrid.Rows.Count.ToString() + ")";
            else
                CanvasDataButton.Text = "Canvas Data";
        }

        private void ExportDataButton_Click(object sender, EventArgs e)
        {
            SquadronContext.ExportDataForm.ShowDialog();
        }

        private void TitleLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (TitleLabel.Tag == null)
                new AboutForm().ShowDialog();
            else
                new AddinInfoForm().Execute(TitleLabel.Tag as SquadronAddin);
        }

        private void clearMenu_Click(object sender, EventArgs e)
        {
            LogBox.Items.Clear();
        }

        private void helpbox_Click(object sender, EventArgs e)
        {
            SquadronAddin addin = (sender as PictureBox).Tag as SquadronAddin;

            if (addin != null)
                if (!string.IsNullOrEmpty(addin.HelpUrl))
                    Process.Start(addin.HelpUrl);
                else
                    Process.Start("http://www.jeanpaulva.com");
        }
    }
}
